<template>
    <div>
        <el-button type="primary" plain @click="add">添加</el-button>
        <el-table
            :data="userlist"
            row-key="id"
            border
            style="width: 100%"
            :tree-props="{ children: 'children' }"
        >
            <el-table-column prop="uid" label="用户编号">
            </el-table-column>
            <el-table-column prop="username" label="用户名称"  width="160"> </el-table-column>
            <el-table-column prop="rolename" label="所属角色"  width="160"> </el-table-column>
            <el-table-column prop="status" label="状态">
                <template slot-scope="scope">
                    <el-tag type="success" plain size="middle">{{
                        scope.row.status == 1 ? "启用" : "禁用"
                    }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" ref="user">
                <template slot-scope="scope">
                    <el-button type="primary" icon="el-icon-edit"  circle @click="edit(scope.row.uid)"></el-button>
                    <el-button type="danger" icon="el-icon-delete" circle @click="del(scope.row.uid)"></el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page.page"
            :page-size="page.size"
            layout="prev, pager, next, jumper,total"
            :total="page.total">
        </el-pagination>
    </div>
</template>

<script>
//引入vuex
import Vuex from "vuex";
export default {
    data() {
        return {};
    },
    methods: {
        //引入 用户列表 接口
        ...Vuex.mapActions({
            getlist: "user/userlist_a",
            userdel: "user/userdelete_a",
            userinfo: "user/userinfo_a",
            usercount:"user/usercount_a",
        }),
        //引入 用户设置
        ...Vuex.mapMutations({
            setusercount:"user/setusercount",
        }),
        //点击添加
        add() {
            this.$emit("changeBtn",true)
        },
        //点击编辑
        edit(uid) {
            this.$emit("changeBtn", false); //点击编辑 不是添加按钮
            this.userinfo(uid);
        },
        //点击删除
        del(uid) {
            //点击弹出
            this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    this.userdel(uid);
                    //判断当时如果页码等于总页码 而且 只有一条数据时候 回退一页页面
                    if(this.page.page == this.page.totalPage && this.userlist.length == 1){
                        this.page.page -=1
                    }
                })
                .catch(() => {
                    this.$message({
                        type: "info",
                        message: "已取消删除",
                    });
                });
        },
        //分页器的函数
        //修改当前查询的条数
        handleSizeChange(){},
        //修改当前页码数
        handleCurrentChange(val){
            this.page.page = val
            // this.setusercount({page:val})
            this.getlist(this.page)
        },
    },
    mounted() {
        //调用
        this.getlist(this.page);
        this.usercount()
    },
    computed: {
        //引入 用户列表数据
        ...Vuex.mapGetters({
            userlist: "user/getuserlist",
            page:"user/getPage"
        }),
    },
};
</script>

<style scoped>
.el-pagination{
    float: right;
}
/deep/ .el-pagination__total{
    margin-left: 20px;
}
</style>
